/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.jee.lib.bo;

import java.util.ArrayList;
import java.util.List;
import uit.jee.lib.access.mapper.AccountMapper;
import uit.jee.lib.access.mapper.ReaderMapper;
import uit.jee.lib.dto.AccountDTO;

/**
 *
 * @author ngloc_it
 */
public class AccountBO {

    public boolean Login(String userName, String password) throws Exception{
        AccountMapper mapper = new AccountMapper();
        return mapper.Login(userName, password);
    }

    /**
     * Get all user in database
     * @return
     * @throws Exception
     */
    public ArrayList<AccountDTO> getAllAccount()throws Exception{
       AccountMapper accountMapper = new AccountMapper();
       ReaderMapper readerMapper = new ReaderMapper();

       ArrayList<AccountDTO> accounts = accountMapper.getAllAccount();

       if((accounts == null) ||
               (
               (accounts != null) && (accounts.isEmpty()))
               ){
           return null;
       }
       int n = accounts.size();
       for(int i = 0; i < n; i++){
           String readerName = readerMapper.GetReaderById(
                                        accounts.get(i).getReaderId()).
                                        getName();
        accounts.get(i).setFullName(readerName);
       }

       return accounts;
    }

    /**
     * Get an account by it's username
     * @param userName
     * @return
     * @throws Exception
     */
    public AccountDTO GetAccount(String userName) throws Exception{
        AccountMapper mapper = new AccountMapper();
        ReaderMapper readerMapper = new ReaderMapper();
        AccountDTO account = mapper.GetAccount(userName);
        String readerName = readerMapper.GetReaderById(
                                        account.getReaderId()).
                                        getName();
        account.setFullName(readerName);

        return account;
    }

    /**
     * Insert an account into database
     * @param account
     * @throws Exception
     */
    public void Insert(AccountDTO account) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Insert(account);
    }

    /**
     * Insert a list of account into database
     * @param accounts list of account for insert
     * @throws Exception
     */
    public void Insert(List<AccountDTO> accounts) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Insert(accounts);
    }

    /**
     *
     * @param account
     * @throws Exception
     */
    public void Update(AccountDTO account) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Update(account);
    }

    /**
     *
     * @param accounts
     * @throws Exception
     */
    public void Update(List<AccountDTO> accounts) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Update(accounts);
    }

    /**
     *
     * @param account
     * @throws Exception
     */
    public void Delete(AccountDTO account) throws Exception{
       AccountMapper mapper = new AccountMapper();
       mapper.Delete(account);
    }

    public void Delete(String id) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Delete(id);
    }

    /**
     *
     * @param accounts
     * @throws Exception
     */
    public void Delete(List<AccountDTO> accounts) throws Exception{
        AccountMapper mapper = new AccountMapper();
        mapper.Delete(accounts);
    }
}
